/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.dialog.schema.view;

import java.awt.Color;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.ResourceBundle;
import javax.swing.text.AttributeSet;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import offset.nodes.client.model.NodeTypeModifications;
import offset.nodes.client.virtual.model.jcr.PrintHandler;
import offset.nodes.client.model.SimpleNamespaceRegistry;
import offset.nodes.client.virtual.model.jcr.nodetype.NodeTypeWriter;
import offset.nodes.client.model.XmlReader;
import org.xml.sax.InputSource;

/**
 *
 * @author Walter Lütgenau
 */
public class NodeTypeDefinitionsWriter {
    public static final Color COLOR_ADDED = Color.BLUE;
    public static final Color COLOR_MODIFIED = Color.BLACK;
    public static final Color COLOR_REMOVED = Color.GRAY;
    
    public static final int MODIFICATIONS_NEW = 1;
    public static final int MODIFICATIONS_MODIFIED = 2;
    public static final int MODIFICATIONS_REMOVED = 4;
    
    NodeTypeModifications modifications;
    int mode;
    
    /** Creates a new instance of NodeTypeDefinitionsDocument */
    public NodeTypeDefinitionsWriter(NodeTypeModifications modifications, int mode) {
        this.modifications = modifications;
        this.mode = mode;
    }
    
    public void write(Document document, ResourceBundle bundle) throws Exception {
        SimpleNamespaceRegistry namespaces = SimpleNamespaceRegistry.getInstance();
        
        SimpleAttributeSet attributes = new SimpleAttributeSet();
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        
        if ( modifications.getAdded().length > 0 && (mode&MODIFICATIONS_NEW) > 0) {
            StyleConstants.setForeground(attributes, COLOR_ADDED);
            StyleConstants.setBold(attributes, true);
            document.insertString(document.getLength(), bundle.getString("schema.write.new") + "\n", attributes);
            StyleConstants.setBold(attributes, false);
            
            NodeTypeWriter.write(buf, modifications.getAdded(), namespaces);
            document.insertString(document.getLength(), buf.toString()+"\n", attributes);
        }
        
        if ( modifications.getModified().length > 0 && (mode&MODIFICATIONS_MODIFIED) > 0 ) {
            StyleConstants.setForeground(attributes, COLOR_MODIFIED);
            StyleConstants.setBold(attributes, true);
            if ( document.getLength() > 0)
                document.insertString(document.getLength(), "\n", attributes);
            document.insertString(document.getLength(), bundle.getString("schema.write.modified") + "\n", attributes);
            StyleConstants.setBold(attributes, false);
            
            buf = new ByteArrayOutputStream();
            NodeTypeWriter.write(buf, modifications.getModified(), namespaces);
            document.insertString(document.getLength(), buf.toString()+"\n", attributes);
        }
        
        if ( modifications.getRemoved().length > 0 && (mode&MODIFICATIONS_REMOVED) > 0 ) {
            StyleConstants.setForeground(attributes, COLOR_REMOVED);
            StyleConstants.setBold(attributes, true);
            if ( document.getLength() > 0)
                document.insertString(document.getLength(), "\n", attributes);
            document.insertString(document.getLength(), bundle.getString("schema.write.removed") + "\n", attributes);
            StyleConstants.setBold(attributes, false);
            
            buf = new ByteArrayOutputStream();
            NodeTypeWriter.write(buf, modifications.getRemoved(), namespaces);
            document.insertString(document.getLength(), buf.toString()+"\n", attributes);
        }
    }
    
}
